/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: TestLogController
 * Author:   zhouheng
 * Date:     2018/6/17 10:20
 * Description: 日志文件测试类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hengby.hengcrud.modular.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.hengby.hengcrud.config.mutidatasource.annotation.DataSource;
import com.hengby.hengcrud.core.common.factory.PageFactory;
import com.hengby.hengcrud.core.common.status.DatasourceEnum;
import com.hengby.hengcrud.core.common.status.LogType;
import com.hengby.hengcrud.core.exception.HengCrudException;
import com.hengby.hengcrud.core.exception.HengCrudExceptionEnum;
import com.hengby.hengcrud.modular.base.controller.BaseController;
import com.hengby.hengcrud.modular.sys.mapper.UserMapper;
import com.hengby.hengcrud.modular.sys.model.OperationLog;
import com.hengby.hengcrud.modular.sys.model.User;
import com.hengby.hengcrud.modular.sys.service.IOperationLogService;
import com.hengby.hengcrud.modular.sys.warpper.LogWarpper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈日志文件测试类〉
 *
 * @author zhouheng
 * @create 2018/6/17
 * @since 1.0.0
 */

@RestController
@RequestMapping("/test")
@Slf4j
public class TestLogController extends BaseController{
    private final Logger logger = LoggerFactory.getLogger(TestLogController.class);

    @Autowired
    UserMapper userMapper;

    @Autowired
    IOperationLogService operationLogService;

    /**
     * 传统方式实现日志
     */
    @RequestMapping(value = "/test1")
    public void test1(){
        logger.debug("debug");//默认日志级别为info
        logger.info("info");
        logger.error("error");
        logger.warn("warn");
    }

    /**
     * Slf4j注解方式实现日志
     */
    @RequestMapping(value = "/test2")
    public void test2(){
        log.debug("debug");//默认日志级别为info
        log.info("info");
        log.error("error");
        log.warn("warn");
    }

    @DataSource(name = DatasourceEnum.DATA_SOURCE_MASTER)
    @RequestMapping(value = "/dataSourceMaster")
    public void testMasterDataSource() throws  Exception{
        User user = new User();
        user.setName("zhouheng");
        user.setRoleid("123");
        userMapper.insert(user);
    }

    @RequestMapping(value = "/dataSourceSlave")
    @DataSource(name = DatasourceEnum.DATA_SOURCE_MASTER)
    public void testSlaveDataSource() throws  Exception{
        User user = new User();
        user.setName("zh69144");
        user.setRoleid("10010");
        userMapper.insert(user);
    }

    // ThreadLocal 模式分页
    @GetMapping("/pagehelper")
    public Object pagehelper(Page page) {
        PageHelper.setPagination(page);
//        page.setRecords(userService.selectList(null));
        page.setTotal(PageHelper.freeTotal());//获取总数并释放资源 也可以 PageHelper.getTotal()
        return page;
    }

    // ThreadLocal 模式分页
    @GetMapping("/page")
    @ResponseBody
    public Object pageTest(@RequestParam(required = false) String beginTime,
                           @RequestParam(required = false) String endTime,
                           @RequestParam(required = false) String logName,
                           @RequestParam(required = false) Integer logType) {
        Page<OperationLog> page = new PageFactory<OperationLog>().defaultPage();
//        PageHelper.setPagination(page);
        List<Map<String, Object>> result = operationLogService.getOperationLogs(page,
                beginTime, endTime, logName, LogType.valueOf(logType),page.getOrderByField(), page.isAsc());
        page.setRecords((List<OperationLog>) new LogWarpper(result).wrap());
//        page.setTotal(PageHelper.freeTotal());//获取总数并释放资源 也可以 PageHelper.getTotal()

        return page;
    }

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(@RequestParam("user") String user){
        if(user.equals("aaa")){
            throw new HengCrudException(HengCrudExceptionEnum.NO_THIS_USER);
        }
        return "Hello World";
    }

    @ResponseBody
    @RequestMapping("/login")
    public void login(){
        throw new AuthenticationException();
    }

}